package cn.donting.jme.edit.event;

import java.util.ArrayList;
import java.util.EventObject;
import java.util.HashMap;

public class EventManager {

    private static HashMap<Class<? extends EventObject>, ArrayList<EventListener>> listeners = new HashMap<>();


    /**
     *
     * @param event
     * @param eventListener
     * @param <T>
     */
    public static  <T extends EventObject> void registerListener(Class<T> event, EventListener<T> eventListener) {
        ArrayList<EventListener> eventListeners;
        if (listeners.containsKey(event)) {
            eventListeners = listeners.get(event);
        }else{
            eventListeners=new ArrayList<>();
            listeners.put(event,eventListeners);
        }
        eventListeners.add(eventListener);
    }

    public static void pushEvent(EventObject eventObject){
        ArrayList<EventListener> eventListeners = listeners.get(eventObject.getClass());
        if(eventListeners==null){
            return;
        }
        for (EventListener eventListener : eventListeners) {
            eventListener.event(eventObject);
        }
    }

}
